﻿using LD.BC;
using LD.Host;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace LD.Tool
{
    public partial class ToolWebServiceFrm : FrmToolBaseSkip//FrmToolBase
    {
        public ToolWebServiceFrm()
        {
            InitializeComponent();
            this.Shown += ToolMoveFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            Hashtable scaners = new Hashtable();
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                if (item.Cells["键"].Value != null &&
                    item.Cells["值"].Value != null
                    )
                {
                    scaners.Add(item.Cells["键"].Value.ToString(),
                                item.Cells["值"].Value.ToString()
                    );
                    var param = new WebServiceParam()
                    {
                        Datas = scaners,
                        InterFace = this.txt_interface.Text,
                        Url = this.txt_url.Text,
                        Tag = this.txt_slot.Text
                    };

                    this.TB_Tool.Param = JsonConvert.SerializeObject(param);
                }
            }
        }

        private void ToolMoveFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }
        private DataTable GetVar()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < Cache.Var.Count; i++)
            {
                dt.Rows.Add(i, Cache.Var[i].Name);
            }
            return dt;
        }
        private void Init()
        {
            DataTable var = GetVar();
            this.值.DataSource = var;
            this.值.ValueMember = "name";
            this.值.DisplayMember = "name";

            if (this.TB_Tool.Param == null)
                return;
            var param = JsonConvert.DeserializeObject<WebServiceParam>(this.TB_Tool.Param);
            if (param != null)
            {
                this.txt_url.Text = param.Url;
                this.txt_interface.Text = param.InterFace;
                this.txt_slot.Text = param.Tag;
                foreach (string item in param.Datas.Keys)
                {
                    int index = this.dgv_data.Rows.Add();
                    this.dgv_data["键", index].Value = item;
                    DataGridViewComboBoxCell cobCellName2 = this.dgv_data["值", index] as DataGridViewComboBoxCell;
                    cobCellName2.Value = param.Datas[item];
                }
            }
        }
        private void tsb_new_Click(object sender, EventArgs e)
        {
            this.dgv_data.Rows.Add();
        }

        private void dgv_data_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (e.ColumnIndex == 0 && this.dgv_data[e.ColumnIndex, 0].Value != null)
            {
                if (int.TryParse(this.dgv_data[e.ColumnIndex, 0].Value.ToString(), out var iotype))
                //&& int.TryParse(this.dgv_data[e.ColumnIndex, 1].Value.ToString(), out var iono))
                {
                    var io = BcFactory.Instance.Config.IoList.Where(p => p.IoType == iotype).ToList();
                    if (io.Any())
                    {
                        DataGridViewComboBoxCell cobCellName1 = this.dgv_data["IO名称", e.RowIndex] as DataGridViewComboBoxCell;
                        cobCellName1.DataSource = io;
                        cobCellName1.DisplayMember = "Name";
                        cobCellName1.ValueMember = "No";
                    }
                    //this.dgv_data[e.ColumnIndex, 1].Value = io.Name;
                }
            }
        }

        private void tsb_sub_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.SelectedRows.Count > 0)
                this.dgv_data.SelectedRows.Clear();
        }
    }
}
